package com.e2oto.seller.modules.seller.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.e2oto.seller.R;
import com.e2oto.seller.common.base.RecycleViewAdapter;
import com.e2oto.seller.common.base.RecycleViewHolder;
import com.e2oto.seller.common.utils.ToastUtils;
import com.e2oto.seller.modules.seller.bean.SpecBean;

import java.util.List;

/**
 * Created by ZhangLiQiang on 2017/11/22.
 */

/**
 * 0代表添加规格的布局
 * 1代表显示规格的布局
 */
public class SpecListAdapter extends RecycleViewAdapter<SpecBean> {


    private Context context;
    private OnClickCommitListener listener;

    public SpecListAdapter(Context context, List<SpecBean> dataList, OnClickCommitListener listener) {
        super(dataList);
        this.context = context;
        this.listener = listener;
    }


    @Override
    public int getLayoutId(int viewType) {
        if (viewType == 0) {
            return R.layout.item_add_spec;
        }
        return R.layout.item_spec_list;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0 && dataList.get(position).getViewType() == 0) {
            return 0;
        }
        return 1;
    }

    @Override
    public void convert(final RecycleViewHolder holder, final SpecBean specBean, final int position) {

        if (getItemViewType(position) == 0) {
            if (listener != null) {
                final EditText unitEt = (EditText) holder.getView(R.id.add_spec_unit);
                final EditText weightEt = (EditText) holder.getView(R.id.add_spec_weight);
                final EditText priceEt = (EditText) holder.getView(R.id.add_spec_price);
                final EditText stockEt = (EditText) holder.getView(R.id.add_spec_stock);
                final CheckBox unitPrice = (CheckBox) holder.getView(R.id.item_spec_unit_price);
                final CheckBox totalPrice = (CheckBox) holder.getView(R.id.item_spec_total_price);
                unitPrice.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (isChecked) {
                            totalPrice.setChecked(false);
                        }
                    }
                });
                totalPrice.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (isChecked) {
                            unitPrice.setChecked(false);
                        }
                    }
                });
                unitEt.setText("");
                weightEt.setText("");
                priceEt.setText("");
                stockEt.setText("");
                holder.getView(R.id.item_add_spec_commit).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String unit = unitEt.getText().toString().trim();
                        String weight = weightEt.getText().toString().trim();
                        String price = priceEt.getText().toString().trim();
                        String stock = stockEt.getText().toString().trim();
                        int type;
                        if (unitPrice.isChecked()) {
                            type = 0;
                        } else {
                            type = 1;
                        }
                        if (TextUtils.isEmpty(unit) ||
                                TextUtils.isEmpty(weight) ||
                                TextUtils.isEmpty(price) ||
                                TextUtils.isEmpty(stock)) {
                            ToastUtils.INSTANCE.showToast(context, "请完善信息");
                            return;
                        }
                        listener.onClick();
                        goneAddLayout();
                        addASpec(unit, weight, price, stock, type);
                    }
                });

            }

        } else {
            final LinearLayout drawer = (LinearLayout) holder.getView(R.id.item_spec_drawer);
            ImageView deleteIv = (ImageView) holder.getView(R.id.item_spec_list_delete);
            ImageView modifyIv = (ImageView) holder.getView(R.id.item_spec_list_modify);
            Button commitBtn = (Button) holder.getView(R.id.item_spec_modify_item_btn);
            TextView priceTv = (TextView) holder.getView(R.id.item_spec_price);
            TextView sizeTv = (TextView) holder.getView(R.id.item_spec_size);
            TextView stockTv = (TextView) holder.getView(R.id.item_spec_stock);
            final EditText unitEt = (EditText) holder.getView(R.id.add_spec_unit);
            final EditText weightEt = (EditText) holder.getView(R.id.add_spec_weight);
            final EditText priceEt = (EditText) holder.getView(R.id.add_spec_price);
            final EditText stockEt = (EditText) holder.getView(R.id.add_spec_stock);
            final CheckBox unitPrice = (CheckBox) holder.getView(R.id.item_spec_unit_price);
            final CheckBox totalPrice = (CheckBox) holder.getView(R.id.item_spec_total_price);
            unitPrice.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        totalPrice.setChecked(false);
                    }
                }
            });
            totalPrice.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        unitPrice.setChecked(false);
                    }
                }
            });
            unitEt.setText("");
            weightEt.setText("");
            priceEt.setText("");
            stockEt.setText("");
            priceTv.setText(specBean.getSell_price());
            sizeTv.setText(specBean.getUnit());
            stockTv.setText(specBean.getStore_nums());

            if (specBean.isShowContent()) {
                drawer.setVisibility(View.VISIBLE);
            } else {
                drawer.setVisibility(View.GONE);
            }
            deleteIv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dataList.remove(holder.getAdapterPosition());
                    notifyItemRemoved(holder.getAdapterPosition());
                }
            });
            modifyIv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    notifyItemChanged(holder.getAdapterPosition());
                    unitEt.setText("");
                    weightEt.setText("");
                    priceEt.setText("");
                    stockEt.setText("");
                    if (drawer.getVisibility() == View.VISIBLE) {
                        specBean.setShowContent(false);
                    } else {
                        specBean.setShowContent(true);
                    }

                }
            });
            commitBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    specBean.setShowContent(false);
                    notifyItemChanged(holder.getAdapterPosition());

                    String unit = unitEt.getText().toString().trim();
                    String weight = weightEt.getText().toString().trim();
                    String price = priceEt.getText().toString().trim();
                    String stock = stockEt.getText().toString().trim();
                    int type;
                    if (unitPrice.isChecked()) {
                        type = 0;
                    } else {
                        type = 1;
                    }
                    if (TextUtils.isEmpty(unit) ||
                            TextUtils.isEmpty(weight) ||
                            TextUtils.isEmpty(price) ||
                            TextUtils.isEmpty(stock)) {
                        ToastUtils.INSTANCE.showToast(context, "请完善信息");
                        return;
                    }
                    modifyASpec(unit, weight, price, stock, type, position);


                }
            });

        }


    }

    public void showAddLayout() {
        SpecBean specBean = new SpecBean();
        specBean.setViewType(0);
        specBean.setShowContent(false);
        dataList.add(0, specBean);
        notifyItemChanged(0);
    }

    public void goneAddLayout() {
        if (dataList != null && dataList.size() != 0 && dataList.get(0).getViewType() == 0) {
            dataList.remove(0);
            notifyItemRemoved(0);

        }
    }

    //type [0:单价;1:总价]
    private void addASpec(String unit, String wigdgt, String price, String stock, int type) {
        SpecBean specBean = new SpecBean();
        specBean.setUnit(unit);
        specBean.setWeight(wigdgt);
        specBean.setSell_price(price);
        specBean.setStore_nums(stock);
        specBean.setViewType(1);
        specBean.setShowContent(false);
        specBean.setType(type);
        dataList.add(specBean);
        notifyItemInserted(dataList.size() - 1);
    }

    //type [0:单价;1:总价]
    private void modifyASpec(String unit, String wigdgt, String price, String stock, int type, int position) {
        SpecBean specBean = new SpecBean();
        specBean.setUnit(unit);
        specBean.setWeight(wigdgt);
        specBean.setSell_price(price);
        specBean.setStore_nums(stock);
        specBean.setType(type);
        specBean.setViewType(1);
        specBean.setShowContent(false);
        dataList.set(position, specBean);
        notifyItemChanged(position);
    }

    public interface OnClickCommitListener {
        void onClick();
    }
}
